﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.IO;

public partial class Members_Uploader : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        btnOK.Attributes.Add("onclick", "opener." + Request.QueryString["OpenerID"] + "_PostBackOnMainPage(); window.close();");
        btnCancel.Attributes.Add("onclick", "window.close()");

        if (!IsPostBack)
        {
            Session["Sort"] = new CompareFileTime();
            BindData();
        }
    }

    private void BindData()
    {
        DirectoryInfo dir = new DirectoryInfo(Server.MapPath(Request.QueryString["Folder"]));
        Array files = dir.GetFiles();

        Array.Sort(files, (IComparer)Session["Sort"]);
        dtlImages.DataSource = files;
        dtlImages.DataBind();
    }

    protected void btnUpload_Click(object sender, EventArgs e)
    {
        if (fileUpload.HasFile)
        {

            string kieufile = Path.GetExtension(fileUpload.FileName).Substring(1, 3);
            if ((kieufile == "jpg") || (kieufile == "jpeg") || (kieufile == "exe") || (kieufile == "jpeg") || (kieufile == "msi") || (kieufile == "pdf") || (kieufile == "doc") || (kieufile == "xls") || (kieufile == "chm") || (kieufile == "png") || (kieufile == "bmp") || (kieufile == "gif") || (kieufile == "rar") || (kieufile == "zip"))
            {
                fileUpload.SaveAs(Server.MapPath(Request.QueryString["Folder"]) + "/" + fileUpload.FileName);
                lblResult.Text = "Upload thành công file: " + fileUpload.FileName;
                lblResult.Visible = true;
            }
            else
            {
                lblResult.Text = "Định dạng file upload " + fileUpload.FileName + " không hợp lệ!.";
                lblResult.Visible = true;
            }
            BindData();
            foreach (DataListItem dli in dtlImages.Items)
            {
                RadioButton radFile = (RadioButton)dli.FindControl("radFile");
                if (radFile.Text == fileUpload.FileName)
                {
                    radFile.Checked = true;
                    Session["SelectedFile"] = Request.QueryString["Folder"] + "/" + radFile.Text;
                    //Session["ImageName"] = radFile.Text;
                }
                else
                {
                    radFile.Checked = false;
                }
            }
        }
    }

    protected void dtlImages_ItemDataBound(object sender, DataListItemEventArgs e)
    {
        ((Image)e.Item.FindControl("image")).ImageUrl = Request.QueryString["Folder"] + "/" + DataBinder.Eval(e.Item.DataItem, "Name");
        ((RadioButton)e.Item.FindControl("radFile")).Text = (string)DataBinder.Eval(e.Item.DataItem, "Name");
        ((Label)e.Item.FindControl("lblSize")).Text = DataBinder.Eval(e.Item.DataItem, "Length").ToString();
        ((Label)e.Item.FindControl("lblDate")).Text = System.Convert.ToDateTime(DataBinder.Eval(e.Item.DataItem, "CreationTime")).ToShortDateString();
    }

    protected void ddlSort_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (ddlSort.SelectedValue == "Time")
        {
            Session["Sort"] = new CompareFileTime();
        }
        else if (ddlSort.SelectedValue == "Name")
        {
            Session["Sort"] = new CompareFileName();
        }
        else if (ddlSort.SelectedValue == "Size")
        {
            Session["Sort"] = new CompareFileSize();
        }

        BindData();
        foreach (DataListItem dli in dtlImages.Items)
        {
            RadioButton radFile = (RadioButton)dli.FindControl("radFile");
            if ((string)Session["SelectedFile"] == Request.QueryString["Folder"] + "/" + radFile.Text)
            {
                radFile.Checked = true;
            }
            else
            {
                radFile.Checked = false;
            }
        }
    }

    protected void radFile_CheckedChanged(object sender, EventArgs e)
    {
        foreach (DataListItem dli in dtlImages.Items)
        {
            RadioButton radFile = (RadioButton)dli.FindControl("radFile");
            if (radFile.ClientID != ((RadioButton)sender).ClientID)
            {
                radFile.Checked = false;
            }
        }
        Session["SelectedFile"] = Request.QueryString["Folder"] + "/" + ((RadioButton)sender).Text;
        //Session["ImageName"] = ((RadioButton)sender).Text;
    }
}

// IComparer for sorting FileInfo[].
class CompareFileTime : IComparer
{
    public int Compare(object x, object y)
    {
        FileInfo file = (FileInfo)x;
        FileInfo file2 = (FileInfo)y;
        return DateTime.Compare(file2.CreationTime, file.CreationTime);
    }
}

class CompareFileName : IComparer
{
    public int Compare(object x, object y)
    {
        FileInfo file = (FileInfo)x;
        FileInfo file2 = (FileInfo)y;
        return string.Compare(file.Name, file2.Name);
    }
}

class CompareFileSize : IComparer
{
    public int Compare(object x, object y)
    {
        FileInfo file = (FileInfo)x;
        FileInfo file2 = (FileInfo)y;
        return file.Length.CompareTo(file2.Length);
    }
}
